// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import 'element-ui/lib/index'
import http from "./util/http";
import store from "./util/store";

Vue.config.productionTip = false
Vue.use(ElementUI)

//在路由之前判断
//to：新路由， from:原路由 ，next:下一个
router.beforeEach((to,from,next)=>{
    document.title=to.meta.title

    if(to.fullPath==="/login"  ||  sessionStorage.getItem("token")){
        next(); // 向下访问新路由
    }else{
        console.log("未登录");
        next({path:"/"});
    }
})

//添加vue的属性
Vue.prototype.$http = http
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>',
  store,
  watch:{
    $route:function (to,from) {
        /*console.log("跳转后的路径");
          console.log(to);
          console.log("跳转之前的路径");
          console.log(from);

          // 判断是否访问的是登录页面
          if (to !="/"){
              let token = sessionStorage.getItem("token");
              if (!token){
                  //替换
                  this.$router.replace({name:"login"});
              }
          }*/
    }
  }
})
